const { Sequelize } = require('sequelize')
const config = require('./config')

const sequelize = new Sequelize(config.MYSQL_DATABASE, config.MYSQL_user, config.MYSQL_PASSWORD, {
    host: 'localhost',
    dialect: 'mysql',
    port: config.MYSQL_PORT,
    query: { raw:true },
    logging: (...msg) => console.log(msg),
    pool: {
        max: 5,
        idle: 30000,
        acquire: 60000
    },
    dialectOptions: {
        charset: 'utf8mb4',
        collate: 'utf8mb4_general_ci'
    },
    define: {
        freezeTableName: true, // 自定义表名
        timestamps: false // 是否自动生产更新时间、创建时间
    }
})
sequelize.authenticate().then(() => {
    console.log('数据库连接成功了');
}).catch((error) => {
    console.log('数据库连接失败', error);
})


module.exports = sequelize


// const mysql = require('mysql2')
// const config = require('./config')
// const connections = mysql.createPool({
//     host: config.MYSQL_HOST,
//     port: config.MYSQL_PORT,
//     database: config.MYSQL_DATABASE,
//     user: config.MYSQL_user,
//     password: config.MYSQL_PASSWORD
// })

// connections.getConnection((err, conn) => {
//     conn.connect((err) => {
//         if (err) {
//             console.log("数据库连接失败~", err)
//         } else {
//             console.log("数据库连接成功了~", err)
//         }
//     })
// })

// module.exports = connections.promise()